package actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbConnectionPool;
import db.DbException;
import forms.formAverage;

public class averageAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException {
			formAverage iform = (formAverage) form;
		try {
			Connection conn = DbConnectionPool.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT grade FROM grade_result WHERE student_id='"+iform.getId()+"'");
			int total = 0;
			int count = 0;
			if(rs != null)
				while(rs.next())
				{
					count++;
					total += Integer.parseInt(rs.getString(1));
				}
			
			conn.commit();
			conn.close();
		} catch (SQLException ex) {
			throw new DbException(ex);
		} catch (NamingException ex) {
			throw new DbException(ex);
		}
		return mapping.findForward("success");
	}
}
